#!/bin/bash

Install_Elasticsearch() {
  Echo_Info "====== Installing Elasticsearch ======"
  Echo_Info "Install ${Elasticsearch_Ver} Version..."
  source /etc/profile
  Echo_Info ${PATH}
  Press_Start
  useradd -d /home/yusp yusp

  cd ${cur_dir}/src
  #修改系统参数
  Elasticsearch_Modify_System_Params
  #下载安装Elasticsearch
  Elasticsearch_Install

  #把Elasticsearch安装为linux服务
  Elasticsearch_Add_AutoStartup
}

Elasticsearch_Modify_System_Params() {
  Echo_Info "====== Elasticsearch_Modify_System_Params ======"
  Echo_Info "修改最大文件句柄数:65535与最大虚拟内存:655360"
  #修改最大文件句柄数
  cat >>/etc/security/limits.conf <<EOF
* soft nproc 65536
* hard nproc 65536
* soft nofile 65536
* hard nofile 65536
EOF
  #加载参数
  source /etc/security/limits.conf

  #修改最大虚拟内存
  cat >>/etc/sysctl.conf <<EOF
fs.file-max=65536	
vm.max_map_count=655360
EOF
  #加载参数
  sysctl -p

}

Elasticsearch_Install() {
  Echo_Info "====== Elasticsearch_Install ======"

  Download_Files ${YUSP_Download_Mirror}/elasticsearch-${Elasticsearch_Ver}.zip elasticsearch-${Elasticsearch_Ver}.zip
  rm -rf ${ELK_Install_Dir}/elasticsearch*
  unzip elasticsearch-6.2.4.zip -d ${ELK_Install_Dir}
  # 修改配置文件，增加跨域，方便使用elasticsearch-head客户端连接
  echo "增加跨域配置，方便使用elasticsearch-head客户端连接"
    cat >>${ELK_Install_Dir}/elasticsearch-${Elasticsearch_Ver}/config/elasticsearch.yml <<EOF
#elasticsearch跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
EOF
  chown yusp:yusp -R ${ELK_Install_Dir}/elasticsearch-${Elasticsearch_Ver}
  #启动elasticsearch
  CURRNET_IP=$(hostname -I)
  Echo_Info "当前主机IP======>${CURRNET_IP}"


  #su - yusp -c "${ELK_Install_Dir}/elasticsearch-${Elasticsearch_Ver}/bin/elasticsearch -E network.host=${CURRNET_IP} -d -p pid"

  # if [ $? -eq 0 ]; then
  # Echo_Green "OK, ====== Elasticsearch_Install Successful!!======"
  # else
  # Echo_Red "Sorry, Elasticsearch_Install failed! please check you operation and try again!"
  # return -1;
  # fi
}

Elasticsearch_Add_AutoStartup() {
  cd ${cur_dir}/src
  \cp ${cur_dir}/init.d/init.d.elasticsearch /etc/init.d/elasticsearch
  chmod +x /etc/init.d/elasticsearch
  sed -i "s#YOURPATH#${PATH}#" /etc/init.d/elasticsearch
  sed -i "s#YOURELK_Install_Dir#${ELK_Install_Dir}#" /etc/init.d/elasticsearch
  sed -i "s#YOURElasticsearch_Ver#${Elasticsearch_Ver}#" /etc/init.d/elasticsearch
  echo "Add Elasticsearch to auto startup..."
  StartUp elasticsearch
  #启动elasticsearch
  /etc/init.d/elasticsearch start
  if [ $? -eq 0 ]; then
    Echo_Green "====== Add Elasticsearch Auto Startup Successful! ======"
    Echo_Green "Elasticsearch installed successfully, enjoy it!"
  else
    Echo_Red "Elasticsearch install failed!"
  fi
}

Uninstall_Elasticsearch() {
  echo "You will uninstall Elasticsearch..."
  echo "Shutting down Elasticsearch..."
  kill $(cat ${ELK_Install_Dir}/elasticsearch-${Elasticsearch_Ver}/pid)
  #Remove_StartUp elasticsearch
  echo "Delete elasticsearch files..."
  rm -rf ${ELK_Install_Dir}/elasticsearch*
  rm -rf /etc/init.d/elasticsearch
  Echo_Green "Uninstall Elasticsearch completed."
}
